package com.ptmall.app.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.bean.ShopCarBean;
import com.ptmall.app.ui.activity.GoodDetilActivity;
import com.ptmall.app.utils.GlideUtil;

import java.util.List;


/**
 * Created by Administrator on 2018/1/12.
 */

public class ShopCarAdapter extends BaseAdapter {

    private Context mContext;
    private List<ShopCarBean> mDataList;
    private LayoutInflater mLayoutInflater;

    ClickInterface clickInterface;


    public void setClickInterface(ClickInterface clickInterface){
        this.clickInterface = clickInterface;
    }

    public ShopCarAdapter(Context context, List<ShopCarBean> list) {
        this.mContext = context;
        this.mDataList = list;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void pushData(List<ShopCarBean> list){
        this.mDataList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mDataList == null){
            return 0;
        }
        return mDataList.size();
    }


    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.item_shopcargood,  parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ShopCarBean bean = mDataList.get(position);

        viewHolder.goodname.setText(bean.product_name);
        viewHolder.goodprice.setText("￥"+bean.sell_price);
        viewHolder.goodguide.setText(bean.specs_name);
        GlideUtil.showImg(mContext,bean.list_img,viewHolder.goodimg);


        if(bean.is_tejia == null||bean.is_tejia.equals("1")){
            viewHolder.activi_rel.setVisibility(View.GONE);
        }else {
            viewHolder.activi_rel.setVisibility(View.VISIBLE);
            viewHolder.activi_text.setText("特价");
        }


        if (!TextUtils.isEmpty(bean.manjian)) {
            viewHolder.activi_rel.setVisibility(View.VISIBLE);
            viewHolder.activi_text.setText(bean.manjian);
        }
        viewHolder.goodimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, GoodDetilActivity.class);
                intent.putExtra("id",bean.productId);
                mContext.startActivity(intent);
            }
        });

        viewHolder.car_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickInterface.chechClick(bean.cartId,position);
//                bean.selected = !bean.selected;
//                notifyDataSetChanged();
            }
        });

        viewHolder.shopcar_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickInterface.shopcarjian(position);
            }
        });

        viewHolder.shopcar_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickInterface.shopcaradd(position);
            }
        });

        viewHolder.shopcar_num.setText(bean.num);

        if(bean.selected){
            viewHolder.car_select.setImageResource(R.drawable.shopcar_true);
        }else {
            viewHolder.car_select.setImageResource(R.drawable.shopcar_unselected);
        }
        //是否全选
        if (clickInterface != null) {
            boolean isSelect = false;
            for (int i = 0; i < mDataList.size(); i++) {
                if (!mDataList.get(i).selected) {
                    isSelect = false;
                    break;
                } else {
                    isSelect = true;
                }
            }
            clickInterface.isAllCheck(isSelect);
        }
        clickInterface.goodprice();

        return convertView;
    }

    class ViewHolder {

        TextView goodname,goodguide,goodprice,shopcar_num,activi_text;
        ImageView goodimg,car_select;
        RelativeLayout shopcar_jia,shopcar_jian;
        RelativeLayout activi_rel;

        ViewHolder(View view) {
            activi_rel = (RelativeLayout) view.findViewById(R.id.activi_rel);
            activi_text = (TextView) view.findViewById(R.id.activi_text);
            goodimg = (ImageView) view.findViewById(R.id.goodimg);
            car_select = (ImageView) view.findViewById(R.id.car_select);
            goodguide = (TextView) view.findViewById(R.id.goodguide);
            goodname = (TextView) view.findViewById(R.id.goodname);
            goodprice = (TextView) view.findViewById(R.id.goodprice);
            shopcar_jia = (RelativeLayout) view.findViewById(R.id.shopcar_jia);
            shopcar_jian = (RelativeLayout) view.findViewById(R.id.shopcar_jian);
            shopcar_num = (TextView) view.findViewById(R.id.shopcar_num);
        }
    }

    public interface ClickInterface{
        void chechClick(String id,int position);
        void isAllCheck(boolean selected);
        void goodprice();
        void shopcaradd(int position);
        void shopcarjian(int position);
    }

}
